package com.lemon.components.ant.project.makers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.project.beans.ControllerInfo;
import com.lemon.components.ant.util.PathUtil;
import com.lemon.components.ant.util.TemplateUtil;
import com.lemon.components.docs.mysql.MysqlLocalTool;
import com.system.core.security.NameUtil;

public class ControllerMaker {
	
	public static void make(ControllerInfo controllerInfo) throws Exception {
		controllerInfo.getServiceInfo().setColumns(
			MysqlLocalTool.getColumnInfo(
					controllerInfo.getServiceInfo().getProjectInfo(), 
					NameUtil.javaName2_(controllerInfo.getServiceInfo().getModelName()))
		);
		String template = controllerInfo.getServiceInfo().getProjectInfo().getTemplatePath();
		String build = PathUtil.getInstance(controllerInfo.getServiceInfo().getProjectInfo()).getControllerPackagePath().getPath()+controllerInfo.getUrlPrefix()+"/"+
				controllerInfo.getServiceInfo().getServiceName();
		String htmlBuild = PathUtil.getInstance(controllerInfo.getServiceInfo().getProjectInfo()).getJavaResourcePath("assemble").getPath()+
				"templates/"+controllerInfo.getUrlPrefix()+"/"+
				controllerInfo.getServiceInfo().getServiceName()+"/"+controllerInfo.getServiceInfo().getServiceName()+"-list.html";
		String jsBuild = PathUtil.getInstance(controllerInfo.getServiceInfo().getProjectInfo()).getJavaResourcePath("assemble").getPath()+
				"public/components/app/"+controllerInfo.getUrlPrefix()+"/"+
				controllerInfo.getServiceInfo().getServiceName()+"/"+controllerInfo.getServiceInfo().getServiceName()+"Page.js";
		
		
		TemplateUtil.getInstance(template).build("/maven-project-template/controller/ControllerTemplate.java", 
				build.toLowerCase() +"/"+ StringUtils.capitalize(controllerInfo.getServiceInfo().getServiceName()) +"Controller.java", controllerInfo);
		TemplateUtil.getInstance(template).build("/maven-project-template/html/boss/ServiceModelPageTemplate.html", 
				htmlBuild, controllerInfo);
		TemplateUtil.getInstance(template).build("/maven-project-template/js/app/ServiceModelPage.js", 
				jsBuild, controllerInfo);
		
		Connection connection = MysqlLocalTool.getCon(controllerInfo.getServiceInfo().getProjectInfo());
		
		PreparedStatement query = connection.prepareStatement("SELECT * FROM sys_menu WHERE menu_name=? AND menu_url=?",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		query.setString(1, controllerInfo.getModelname());
		query.setString(2, "/"+controllerInfo.getUrlPrefix()+"/"+controllerInfo.getBaseUrl()+"/"+"to_page");
		ResultSet queryResult = query.executeQuery();
		queryResult.last();
		if(queryResult.getRow() == 0) {
			PreparedStatement ps = connection.prepareStatement("INSERT INTO `sys_menu` (`parent_id`, `menu_name`, `menu_icon`, `menu_url`) VALUES "
					+ "(?, ?, 'layui-icon-app', ?)");
			ps.setLong(1, controllerInfo.getParentMenuId());
			ps.setString(2, controllerInfo.getModelname());
			ps.setString(3, "/"+controllerInfo.getUrlPrefix()+"/"+controllerInfo.getBaseUrl()+"/"+"to_page");
			ps.executeUpdate();
		}
		
		PreparedStatement resourceStatement = connection.prepareStatement("INSERT INTO `sys_resource` (`parent_resource_id`, `name`, `type`, `spread`) VALUES (?, ?, 'operation', '1')",Statement.RETURN_GENERATED_KEYS);
		resourceStatement.setLong(1, controllerInfo.getParentResourceId());
		resourceStatement.setString(2, controllerInfo.getModelname());
		resourceStatement.execute();
		ResultSet resultSet = resourceStatement.getGeneratedKeys();
		Long id = 0L;
		if(resultSet.next()) {
			id = resultSet.getLong(1);
		}
		System.out.println(id);
		connection.setAutoCommit(false);
		Statement stmt = connection.createStatement();
		String uncapServiceName = StringUtils.uncapitalize(controllerInfo.getServiceInfo().getServiceName());
		stmt.addBatch("INSERT INTO `sys_resource` (`parent_resource_id`, `name`, `type`, `permission`, `url`, `spread`) VALUES ("+id+", '列表页', 'url', '"+ uncapServiceName +"_topage"+"', '/"+controllerInfo.getUrlPrefix()+"/"+controllerInfo.getBaseUrl()+"/to_page', '1')");
		stmt.addBatch("INSERT INTO `sys_resource` (`parent_resource_id`, `name`, `type`, `permission`, `url`, `spread`) VALUES ("+id+", '新增操作', 'operation', '"+ uncapServiceName+"_add_opt"+"', null, '1')");
		stmt.addBatch("INSERT INTO `sys_resource` (`parent_resource_id`, `name`, `type`, `permission`, `url`, `spread`) VALUES ("+id+", '删除操作', 'operation', '"+ uncapServiceName +"_delete_opt"+"', null, '1')");
		stmt.addBatch("INSERT INTO `sys_resource` (`parent_resource_id`, `name`, `type`, `permission`, `url`, `spread`) VALUES ("+id+", '编辑操作', 'operation', '"+ uncapServiceName +"_edit_opt"+"', null, '1')");
		stmt.executeBatch();
		connection.commit();
		connection.close();
	}
	
}
